{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Distribute Money to Maximum Children"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: distMoney"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #将钱分给最多的儿童"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数&nbsp;<code>money</code>&nbsp;，表示你总共有的钱数（单位为美元）和另一个整数&nbsp;<code>children</code>&nbsp;，表示你要将钱分配给多少个儿童。</p>\n",
    "\n",
    "<p>你需要按照如下规则分配：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有的钱都必须被分配。</li>\n",
    "\t<li>每个儿童至少获得&nbsp;<code>1</code>&nbsp;美元。</li>\n",
    "\t<li>没有人获得 <code>4</code>&nbsp;美元。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你按照上述规则分配金钱，并返回 <strong>最多</strong>&nbsp;有多少个儿童获得 <strong>恰好</strong><em>&nbsp;</em><code>8</code>&nbsp;美元。如果没有任何分配方案，返回&nbsp;<code>-1</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>money = 20, children = 3\n",
    "<b>输出：</b>1\n",
    "<b>解释：</b>\n",
    "最多获得 8 美元的儿童数为 1 。一种分配方案为：\n",
    "- 给第一个儿童分配 8 美元。\n",
    "- 给第二个儿童分配 9 美元。\n",
    "- 给第三个儿童分配 3 美元。\n",
    "没有分配方案能让获得 8 美元的儿童数超过 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>money = 16, children = 2\n",
    "<b>输出：</b>2\n",
    "<b>解释：</b>每个儿童都可以获得 8 美元。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= money &lt;= 200</code></li>\n",
    "\t<li><code>2 &lt;= children &lt;= 30</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [distribute-money-to-maximum-children](https://leetcode.cn/problems/distribute-money-to-maximum-children/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [distribute-money-to-maximum-children](https://leetcode.cn/problems/distribute-money-to-maximum-children/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['20\\n3', '16\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        if children * 7 < money:\n",
    "            return children - 1\n",
    "        if children * 7 == money:\n",
    "            return children\n",
    "        ans = money // 7\n",
    "        return ans - 1 if ans + 1 == children and money % 7 == 3 else ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7, children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money = money - children # 每人分1r\n",
    "        # 尽可能分配7r\n",
    "        ans = min(money // 7, children)\n",
    "        money -= 7 * ans\n",
    "        \n",
    "\n",
    "        remain = children - ans\n",
    "        if (remain == 0 and money > 0) or (remain == 1 and money == 3)  :\n",
    "            return ans - 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money -= children\n",
    "        if money < 0: return -1\n",
    "\n",
    "        ans = min(money // 7, children)\n",
    "        money -= 7 * ans\n",
    "        children -= ans\n",
    "\n",
    "        if (children == 0 and money) or (children == 1 and money == 3):\n",
    "            ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        return -1 if children > money else children if money == (children << 3) else children - 2 if money == (children << 3) - 4 else min(~~(money - children) // 7, children - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money <children:\n",
    "            return -1\n",
    "        res = money - children\n",
    "        ans = res // 7\n",
    "        if ans > children:\n",
    "            return children - 1\n",
    "        elif ans == children:\n",
    "            if res % 7 == 0:\n",
    "                return  children\n",
    "            else:\n",
    "                return children - 1\n",
    "        elif ans == 0:\n",
    "            return 0\n",
    "        elif ans == children - 1 and res % 7 == 3:\n",
    "            return children - 2\n",
    "        else:\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7, children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money-=children\n",
    "        if money<0: return -1\n",
    "        return min(children,money//7)-(money>7*children or money==(children-1)*7+3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7, children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "      if money<children:\n",
    "            return -1\n",
    "      money-=children\n",
    "      cnt=min(money//7,children)\n",
    "      money-=cnt*7\n",
    "      children-=cnt\n",
    "      if(children==0 and money>0) or (children==1 and money==3):\n",
    "          cnt-=1\n",
    "      return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money -= children  # 每人至少 1 美元\n",
    "        if money < 0: return -1\n",
    "        ans = min(money // 7, children)  # 初步分配，让尽量多的人分到 8 美元\n",
    "        money -= ans * 7\n",
    "        children -= ans\n",
    "        # children == 0 and money：必须找一个前面分了 8 美元的人，分配完剩余的钱\n",
    "        # children == 1 and money == 3：不能有人恰好分到 4 美元\n",
    "        if children == 0 and money or \\\n",
    "           children == 1 and money == 3:\n",
    "            ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children: return -1\n",
    "        rest = money\n",
    "        ans = 0\n",
    "        for i in range(children):\n",
    "            min_res = children-i-1\n",
    "            max_allocate = rest-min_res\n",
    "            if max_allocate >= 8:\n",
    "                if i != children-1 or (i == children-1 and max_allocate == 8):\n",
    "                    ans += 1\n",
    "                    rest -= 8\n",
    "            elif max_allocate==4 and i==children-1:\n",
    "                ans -= 1\n",
    "                rest -= 4\n",
    "            else:\n",
    "                rest -= max_allocate \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children: return -1\n",
    "\n",
    "        count = children\n",
    "        for c in range(count, -1, -1):\n",
    "            if c * 8 > money: \n",
    "                continue\n",
    "            if c * 8 < money and c == children:\n",
    "                continue\n",
    "            if money - c*8 == 4 and c == children-1: \n",
    "                continue\n",
    "            if money == c*8 and c != children:\n",
    "                continue\n",
    "            if money - c*8 < children-c:\n",
    "                continue\n",
    "            break\n",
    "        \n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money -= children\n",
    "        if money < 0:\n",
    "            return -1\n",
    "        # 先继续分完得到初步答案，再讨论是否需要更新\n",
    "        ans = min(money // 7, children)\n",
    "        money -= ans * 7\n",
    "        children -= ans\n",
    "        if money and children == 0 or \\\n",
    "            money == 3 and children == 1:\n",
    "            ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money<children:\n",
    "            return -1\n",
    "        money-=children\n",
    "        a=min(money//7,children)\n",
    "        money-=a*7\n",
    "        children-=a\n",
    "        if children==0 and money>0:\n",
    "            a-=1\n",
    "        elif children==1 and money==3:\n",
    "            a-=1\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        ans = 0\n",
    "        for i in range(children + 1):\n",
    "            left = money - i * 8\n",
    "            left_c = children - i\n",
    "            if left < 0:\n",
    "                break\n",
    "            if left < left_c:\n",
    "                break\n",
    "            if left == 4 and left_c == 1:\n",
    "                continue\n",
    "            if left and not left_c or not left and left_c:\n",
    "                continue\n",
    "            ans = i\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "\n",
    "        if children - money > 0:\n",
    "            return -1\n",
    "\n",
    "        money -= children\n",
    "        d, m = divmod(money, 7)\n",
    "\n",
    "        if d:\n",
    "            if d > children:\n",
    "                return children-1\n",
    "            if (d == children - 1 and m == 3) or (d==children and m):\n",
    "                return d-1\n",
    "            return d\n",
    "\n",
    "\n",
    "\n",
    "        return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        res = 0\n",
    "        while money >= 7 + children > 7:\n",
    "            money -= 8\n",
    "            children -= 1\n",
    "            res += 1\n",
    "        if (money == 4 and children == 1) or (money > 0 and children == 0):\n",
    "            res -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > children * 8:\n",
    "            return children - 1\n",
    "        if money == children * 8 -4:\n",
    "            return children - 2\n",
    "        return (money - children) //7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7,children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (money > 0 and children == 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money - (children - 1) * 8 == 4:\n",
    "            return max((money - children) // 7 - 1, 0)\n",
    "        elif money - (children - 1) * 8 > 8:\n",
    "            return children -1\n",
    "        return (money - children) // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        pre=children\n",
    "        if money>=children*8:\n",
    "            if money==children*8:\n",
    "                return children\n",
    "            children-=1\n",
    "        if children>money:\n",
    "            return -1       \n",
    "        while money<children*8:\n",
    "            if children==0:\n",
    "                return children\n",
    "            children-=1\n",
    "        while children*8+pre-children>money:\n",
    "            if children==0:\n",
    "                return children\n",
    "            children-=1\n",
    "        while money-children*8==4 and pre-children==1:\n",
    "            children-=1\n",
    "        return children\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1     \n",
    "        elif 8 * children < money:\n",
    "            return children - 1\n",
    "        elif money - 8 * (children - 1) == 4:\n",
    "            return children - 2\n",
    "        else:\n",
    "            return (money - children) // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children :\n",
    "           return -1\n",
    "        money -=children \n",
    "        ans = min (money//7,children )\n",
    "        money -= ans *7\n",
    "        children -= ans \n",
    "        if (children == 0 and money >0) or (children ==1 and money ==3):\n",
    "             ans -=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money == (children - 1) * 8 + 4:\n",
    "            return children - 2\n",
    "        if money > children * 8:\n",
    "            return children - 1\n",
    "        return (money - children) // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt= min(money//7, children)\n",
    "        money -= cnt*7\n",
    "        children -= cnt\n",
    "        if (children==0 and money > 0) or (children== 1 and money== 3):\n",
    "            cnt -= 1\n",
    "        return cnt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money < children + 7:\n",
    "            return 0\n",
    "        max_money = money - children\n",
    "        max_count = floor(max_money/7)\n",
    "        num = max_money % 7\n",
    "        print(\"-------\", max_count, num, money - max_count*8)\n",
    "        if (num == 3 and children - max_count==1):\n",
    "            max_count = max_count -1\n",
    "        if max_count == children and money - max_count*8 :\n",
    "            max_count = max_count -1\n",
    "        if max_count > children:\n",
    "            max_count = children -1\n",
    "        return max_count        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children: return -1\n",
    "\n",
    "        remaining = money - children\n",
    "        nums = remaining // 7\n",
    "        left = remaining - nums * 7\n",
    "\n",
    "        if nums > children: return children - 1\n",
    "        elif nums == children:\n",
    "            return children if left == 0 else children -1\n",
    "        else:\n",
    "            if children - 1 == nums and left == 3:\n",
    "                return nums-1\n",
    "\n",
    "            return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        ans = 0\n",
    "        while money >= 7 and children > 1:\n",
    "            money -= 7\n",
    "            children -= 1\n",
    "            ans += 1\n",
    "        if money == 3 and children == 1:\n",
    "            ans -= 1\n",
    "        elif money == 7 and children == 1:\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money < children + 7:\n",
    "            return 0\n",
    "        max_money = money - children\n",
    "        max_count = floor(max_money/7)\n",
    "        num = max_money % 7\n",
    "\n",
    "        if (num == 3 and children - max_count==1):\n",
    "            max_count = max_count -1\n",
    "        if max_count == children and money - max_count*8 :\n",
    "            max_count = max_count -1\n",
    "        if max_count > children:\n",
    "            max_count = children -1\n",
    "        return max_count        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:return -1\n",
    "        money -= children\n",
    "        res = min(money // 7, children)\n",
    "        money -= res * 7\n",
    "        children -= res\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            res -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        ans = 0\n",
    "        while money >= 7 and children > 1:\n",
    "            money -= 7\n",
    "            children -= 1\n",
    "            ans += 1\n",
    "        if money == 3 and children == 1:\n",
    "            ans -= 1\n",
    "        elif money == 7 and children == 1:\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        n = money - children\n",
    "        if n < 0:\n",
    "            return -1\n",
    "        else:\n",
    "            k, d = n // 7, n % 7\n",
    "            if d == 3:\n",
    "                if children - k == 1:\n",
    "                    return children - 2\n",
    "                elif children - k <= 0:\n",
    "                    return children - 1\n",
    "                elif children - k > 1:\n",
    "                    return k\n",
    "            else:\n",
    "                if k < children or (k == children and d == 0):\n",
    "                    return k\n",
    "                else:\n",
    "                    return children - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children*8<money:\n",
    "            return children-1\n",
    "        money-=children\n",
    "        i=0\n",
    "        if money<0:\n",
    "            return -1\n",
    "        while money>6:\n",
    "            i+=1\n",
    "            money-=7\n",
    "        if i==children-1 and money==3:\n",
    "            return i-1\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7, children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children>money:\n",
    "            return -1\n",
    "        elif children+7>money:\n",
    "            return 0\n",
    "        else:\n",
    "            money-=children\n",
    "            ans=min(children,money//7)\n",
    "            children-=ans\n",
    "            money-=7*ans\n",
    "            if (children==1 and money==3) or (children==0 and money):\n",
    "                ans-=1\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:return -1\n",
    "        money-=children\n",
    "        res=min(money // 7,children)\n",
    "        money-=res*7\n",
    "        children-=res\n",
    "        if (children==0 and money>0)or(children==1 and money==3):\n",
    "            res-=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money = money -children\n",
    "        count = 0\n",
    "        while money >= 7:\n",
    "            money -= 7\n",
    "            count += 1\n",
    "        if count > children:\n",
    "            return children-1\n",
    "        if count == children and money>0:\n",
    "            return children-1\n",
    "        if count == children-1 and money == 3:\n",
    "            return count-1\n",
    "        if money< 0:\n",
    "            return -1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        # print(money)\n",
    "        cnt = min(money // 7, children)\n",
    "        # print(cnt)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        # print(money)\n",
    "        # print(children)\n",
    "        if money == 3 and children == 1 or money > 0 and children == 0:\n",
    "            cnt -= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children > money:\n",
    "            return -1\n",
    "        n = min((children, (money - children) // 7))\n",
    "        if ((money - children) // 7 ) > children:\n",
    "            return n - 1\n",
    "        else:\n",
    "            if((money - children) % 7 == 3) and (n == children -1):\n",
    "                return n - 1\n",
    "            if((money - children) % 7 != 0) and (n == children):\n",
    "                return n - 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money>children*8:\n",
    "            return children-1\n",
    "        for i in range(children+1):\n",
    "            cur=money-i*8\n",
    "            if cur<children-i or (cur==4 and i==children-1):\n",
    "                return i-1\n",
    "        return children"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money>8*children:\n",
    "            return children-1\n",
    "        if money==8*children:\n",
    "            return children\n",
    "        diff=8*children-money\n",
    "        if diff==4:\n",
    "            return children-2\n",
    "        r=children-(diff+6)//7\n",
    "        if r<0:\n",
    "            return -1\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        # 先给每个孩子分配 1 美元\n",
    "        money -= children\n",
    "        if money < 0:\n",
    "            return -1\n",
    "        ans = min(money // 7, children) # 尽可能给孩子 7 美元\n",
    "        money -= ans * 7 # 剩余的钱\n",
    "        children -= ans # 剩余的孩子(只被分配了 1 美元的孩子)\n",
    "        if money and children == 0 or children == 1 and money == 3:\n",
    "            ans -= 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money>8*children:\n",
    "            return children-1\n",
    "        if money==8*children:\n",
    "            return children\n",
    "        diff=8*children-money\n",
    "        if diff==4:\n",
    "            return children-2\n",
    "        r=children-(diff+6)//7\n",
    "        if r<0:\n",
    "            return -1\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children + 7 and money >= children:\n",
    "            return 0\n",
    "        elif money < children:\n",
    "            return -1\n",
    "        elif money > 8*children:\n",
    "            return children-1\n",
    "        else:\n",
    "            money -= children\n",
    "            if money % 7 == 3 and (money-3) / 7 == children-1:\n",
    "                return children-2\n",
    "            else:\n",
    "                return money // 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        res = min(money//7,children)\n",
    "        money -= res * 7\n",
    "        children -= res\n",
    "        if children == 0 and money > 0 or children == 1 and money == 3:\n",
    "            res -= 1\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        rest = money - children\n",
    "        if rest < 0 :\n",
    "            return -1\n",
    "        ans = rest // 7\n",
    "        r = rest % 7\n",
    "        if children - ans == 1 and r == 3:\n",
    "            ans -= 1\n",
    "        if ans > children:\n",
    "            ans = children - 1\n",
    "        if ans == children and r > 0:\n",
    "            ans = children - 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        k=int(money/8)\n",
    "        if money<children:\n",
    "            return -1\n",
    "        if k==children and money-8*k==0:\n",
    "            return k\n",
    "        elif k>=children:\n",
    "            return children-1\n",
    "        else:\n",
    "            if money-k*8<children-k:\n",
    "                return int(k-(children-k-money+k*8)/7)\n",
    "            elif money-k*8==4 and children-k==1:\n",
    "                return k-1\n",
    "            else:\n",
    "                return k\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money-=children\n",
    "        cnt=min(money//7,children)\n",
    "        money-=cnt*7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money>0) or (children==1 and money == 3):\n",
    "            cnt-=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money -children < 0:\n",
    "            return -1\n",
    "\n",
    "        else:\n",
    "            money = money-children\n",
    "            if money == 0:\n",
    "                return 0\n",
    "\n",
    "            else:\n",
    "                list = [1]* children\n",
    "                s = []\n",
    "                for i in list:\n",
    "                    money -= 7\n",
    "                    children -= 1   \n",
    "                    if money >=0:\n",
    "                        s.append(1)\n",
    "                    if children == 1 and money == 3:\n",
    "                        s.pop(0)\n",
    "                    if children ==0 and money >0:\n",
    "                        s.pop(0)\n",
    "                return len(s)\n",
    "                \n",
    "                return len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money<children:return -1\n",
    "        money -= children\n",
    "        cnt = min(money//7,children)\n",
    "        money -= cnt*7\n",
    "        children -= cnt\n",
    "        if (children==0 and money>0) or (children==1 and money==3):\n",
    "            cnt -= 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        #贪心算法\n",
    "        # 要让尽量多的孩子获得正好8美元，但是不能有人拥有4美元\n",
    "        # 两种情况：\n",
    "        # 1.money > children * 8 ,那么把多余的钱给其中一个人就行了，获得8的人数是总人数-1\n",
    "        # 2.monry < children * 8,先确保每个人都有钱，\n",
    "        # money - children, 然后每个人发钱，能发到8就发到8（贪心），不能发到8就跳过，剩下的钱如果刚好是3，且最后不是8的人只剩一个，那只能牺牲一个领到8的人和其分摊3块钱不然会出现领取4块钱的人\n",
    "        #先做边界值判断\n",
    "        #钱比人少\n",
    "        if money<children:\n",
    "            return -1\n",
    "        if money > 8 * children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # money-8x >= children-x, x <= (money-children)/7\n",
    "        return (money - children) // 7\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        if money % 7 == 0:\n",
    "            if money // 7 <= children:\n",
    "                return money // 7\n",
    "            elif money // 7 > children:\n",
    "                return children - 1\n",
    "        elif money % 7 == 3:\n",
    "            if money // 7 == 0:\n",
    "                if children == 1:\n",
    "                    return -1\n",
    "                else:\n",
    "                    return 0\n",
    "            if money // 7 >= children:\n",
    "                return children - 1\n",
    "            if money // 7 == children - 1:\n",
    "                return money // 7 - 1\n",
    "            if money // 7 < children - 1:\n",
    "                return money // 7\n",
    "        elif money % 7 != 3:\n",
    "            if money // 7 >= children:\n",
    "                return children - 1\n",
    "            else:\n",
    "                return money // 7\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money<children:\n",
    "            return -1\n",
    "        if money>children*8:\n",
    "            return children-1\n",
    "        money-=children\n",
    "        ans=money//7\n",
    "        if ans>children:\n",
    "            return 0\n",
    "        elif ans==children:\n",
    "            return children\n",
    "        else:\n",
    "            if money%7==3 and children-ans==1:\n",
    "                return max(ans-1,0)\n",
    "            else:\n",
    "                return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        if money > 8*children:\n",
    "            return children - 1\n",
    "        if money == 8 * children - 4:\n",
    "            return children - 2\n",
    "        # 假设x个儿童获得了8美元，剩下的钱够分配给剩下的儿童就行\n",
    "        # money - 8 * x >= children - x\n",
    "        return (money - children) // 7    \n",
    "            \n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        money -= children\n",
    "        if money < 0:\n",
    "            return -1\n",
    "\n",
    "        ans = min(money // 7, children)\n",
    "\n",
    "        money -= ans * 7\n",
    "        children -= ans\n",
    "\n",
    "        if children == 0 and money > 0:\n",
    "            ans -= 1\n",
    "        elif children == 1 and money == 3:\n",
    "            ans -= 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children: return -1\n",
    "        money -= children\n",
    "        res = min(money // 7, children)\n",
    "        money -= res * 7\n",
    "        children -= res\n",
    "        if (children == 0 and money > 0) or (children == 1 and money ==3):\n",
    "            res -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children*8<money:\n",
    "            return children-1\n",
    "        if children*8==money:\n",
    "            return children\n",
    "        if money<children:\n",
    "            return -1\n",
    "        # 保证 children < money < 8*children\n",
    "        assignment = [1]*children\n",
    "        money -= children\n",
    "        cnt = 0\n",
    "        for i in range(children):\n",
    "            if money==3:\n",
    "                # 当前剩余 money = 3\n",
    "                # 如果前面还有多个孩子可以分配\n",
    "                if cnt+1<children:\n",
    "                    break\n",
    "                # cnt+1 == children 前面没有多的孩子可以分配了，要把一部分钱往回分配，所以个数要-1\n",
    "                cnt -= 1\n",
    "                break\n",
    "            elif money<7:\n",
    "                break\n",
    "            # money > 7:\n",
    "            money -= 7\n",
    "            cnt += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children > money:\n",
    "            return -1\n",
    "        n = min((children, (money - children) // 7))\n",
    "        if ((money - children) // 7 ) > children:\n",
    "            return n - 1\n",
    "        else:\n",
    "            if((money - children) % 7 == 3) and (n == children -1):\n",
    "                return n - 1\n",
    "            if((money - children) % 7 != 0) and (n == children):\n",
    "                return n - 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if children > money:\n",
    "            return -1\n",
    "        n = min((children, (money - children) // 7))\n",
    "        if ((money - children) // 7 ) > children:\n",
    "            return n - 1\n",
    "        else:\n",
    "            if((money - children) % 7 == 3) and (n == children -1):\n",
    "                return n - 1\n",
    "            if((money - children) % 7 != 0) and (n == children):\n",
    "                return n - 1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children: return -1\n",
    "        money_remain = money - children\n",
    "        count = min(money_remain // 7, children)\n",
    "        money_remain -= 7 * count\n",
    "        children -= count\n",
    "        if (children == 0 and money_remain > 0) or (children == 1 and money_remain == 3):\n",
    "            count -= 1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distMoney(self, money: int, children: int) -> int:\n",
    "        if money < children:\n",
    "            return -1\n",
    "        money -= children\n",
    "        cnt = min(money // 7, children)\n",
    "        money -= cnt * 7\n",
    "        children -= cnt\n",
    "        if (children == 0 and money > 0) or (children == 1 and money == 3):\n",
    "            cnt -= 1\n",
    "        return cnt\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
